package caidan;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import caidan.data.FoodData;
import jinyilw.swing.visuals.Jcomp.JImageComp;

public class FoodComp extends JImageComp
{
	private static final long serialVersionUID = -4378444722865814536L;
	protected final JLabel foodNameLabel = new JLabel();
	protected FoodData foodData;
	protected PositionConfiger positionConfiger;

	public FoodComp(PositionConfiger p)
	{
		positionConfiger = p;
		setBorder(PositionConfiger.border);
		setLayout(null);
		foodNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
		foodNameLabel.setVerticalAlignment(SwingConstants.TOP);
		add(foodNameLabel);
	}

	public FoodComp(FoodData fData, PositionConfiger p)
	{
		positionConfiger = p;
		// setBorder(PositionConfiger.border);
		setLayout(null);
		foodNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
		foodNameLabel.setVerticalAlignment(SwingConstants.TOP);
		add(foodNameLabel);

		foodData = fData;
		setImage(fData.getThumbnail());
		foodNameLabel.setText(fData.getFoodName());
		foodData.addNameChangedOperater((String o) ->
		{
			foodNameLabel.setText(o);
		});
	}

	public FoodData getFoodData()
	{
		return foodData;
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		int w = getWidth(), h = getHeight();
		super.setBounds(x, y, width, height);
		if (w == width && h == height || positionConfiger == null)
			return;
		h = positionConfiger.textHeight;
		foodNameLabel.setBounds(0, height - h, width - 2, h);
		foodNameLabel.setFont(positionConfiger.textFont);
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		if (image != null && positionConfiger != null)
		{
			Shape savedClip = g.getClip();
			((Graphics2D) g).clip(positionConfiger.roundShape);
			int x = (int) positionConfiger.roundShape.x,
					y = (int) positionConfiger.roundShape.y;
			int w = (int) positionConfiger.roundShape.width,
					h = (int) positionConfiger.roundShape.height;
			g.drawImage(image, x, y, w, h - positionConfiger.height, this);
			g.setClip(savedClip);
		}
	}
}
